package database;

import model.User;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.webkit.WebChromeClient.CustomViewCallback;

public class Database extends SQLiteOpenHelper {

	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_NAME = "smartnote";

	// ---table user
	private static final String TABLE_USER = "user";
	private static final String KEY_USER_USERID = "userid";
	private static final String KEY_USER_NICKNAME = "nickname";
	private static final String KEY_USER_BIRTHDAY = "birthday";
	private static final String KEY_USER_GENDER = "gender";
	private static final String KEY_USER_TYPE = "type";
	private static final String KEY_USER_EMAIL = "email";
	private static final String KEY_USERNAME = "username";
	private static final String KEY_TIME = "time";
	private static final String KEY_LOGO = "logo";

	//table group
	private static final String TABLE_GROUP = "groups";
	private static final String KEY_GROUP_GROUPID = "groupid";
	private static final String KEY_GROUP_CATID = "catid";
	private static final String KEY_GROUP_TITLE = "title";
	private static final String KEY_GROUP_LOGOID = "logoid";
	private static final String KEY_GROUP_MONITOR = "monitor";
	private static final String KEY_GROUP_TIMECREATE = "timecreate";
	private static final String KEY_GROUP_PUBLISH = "publish";
	private static final String KEY_GROUP_TIMEEXPIRED = "timeexpired";
	private static final String KEY_GROUP_CATNAME = "catname";
	private static final String KEY_GROUP_TOTALOFMEM = "totalofmem";
	private static final String KEY_GROUP_LOGOLINK = "logolink";
	private static final String KEY_GROUP_NEWS = "numnews";
	private static final String KEY_GROUP_GROUPUSERID = "groupuserid";

	public Database(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// create table user
		String SQL_String_User = "CREATE TABLE " + TABLE_USER + "("
				+ KEY_USER_USERID + " TEXT PRIMARY KEY," + KEY_USER_NICKNAME
				+ " TEXT," + KEY_USERNAME + " TEXT,"+ KEY_USER_EMAIL + " TEXT," + KEY_USER_GENDER
				+ " TEXT," + KEY_USER_TYPE + " TEXT,"
				+ KEY_TIME + " Date,"
				+ KEY_LOGO + " TEXT,"
				+ KEY_USER_BIRTHDAY + " TEXT" + ")";
		db.execSQL(SQL_String_User);	
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
		
		onCreate(db);
	}

	//table user
	public void setUser(User u) {
		resetUser();
		SQLiteDatabase db = this.getWritableDatabase();
		String nullColumnHack = null; // chấp nhận field có giá trị null
		ContentValues values = new ContentValues();
		values.put(KEY_USER_BIRTHDAY, u.getBirthday());
		values.put(KEY_USER_USERID, u.getUserId());
		values.put(KEY_USER_NICKNAME, u.getNickName());
		values.put(KEY_USERNAME, u.getUserName());
		values.put(KEY_USER_TYPE, u.getType());
		values.put(KEY_USER_GENDER, u.getGender());
		values.put(KEY_USER_EMAIL, u.getEmail());
		values.put(KEY_LOGO, u.getLogo());
		values.put(KEY_TIME, u.getTime());
		db.insert(TABLE_USER, nullColumnHack, values);
		db.close();
	}

	public User getUser() {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from " + TABLE_USER, null);
		if (cursor.moveToFirst()) {
			String id = cursor.getString(0);
			String nickname = cursor.getString(1);
			String userName = cursor.getString(2);
			String email = cursor.getString(3);
			String gender = cursor.getString(4);
			String type = cursor.getString(5);
			String time = cursor.getString(6);
			String birthday = cursor.getString(8);
			String logo = cursor.getString(7);

			User u = new User(id, userName, nickname, gender, birthday, type, email, time,logo);
			db.close();
			return u;
		}
		db.close();
		return null;
	}
	public void resetUser()
	{
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_USER, null, null);
		db.close();
	}	
	//---end table user
	
}
